package com.ruoyi.project.module.packetConfig.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.PropertiesFileUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.packetConfig.domain.PacketConfig;
import com.ruoyi.project.module.packetConfig.service.IPacketConfigService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 定时发红包配置 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-08-23
 */
@Controller
@RequestMapping("/module/packetConfig")
public class PacketConfigController extends BaseController
{
    private String prefix = "module/packetConfig";
	
	@Autowired
	private IPacketConfigService packetConfigService;
	
	@RequiresPermissions("module:packetConfig:view")
	@GetMapping()
	public String packetConfig()
	{
	    return prefix + "/packetConfig";
	}
	
	/**
	 * 查询定时发红包配置列表
	 */
	@RequiresPermissions("module:packetConfig:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PacketConfig packetConfig)
	{
		startPage();
        List<PacketConfig> list = packetConfigService.selectPacketConfigList(packetConfig);
		return getDataTable(list);
	}
	
	/**
	 * 新增定时发红包配置
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存定时发红包配置
	 */
	@RequiresPermissions("module:packetConfig:add")
	@Log(title = "定时发红包配置", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PacketConfig packetConfig,@RequestParam(required = true) String number )
	{
		String packeConfigtId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		String system_userId = PropertiesFileUtil.getInstance("hongbao").get("system_userId");
		packetConfig.setRedPacketConfigId(packeConfigtId);
		packetConfig.setRedPacketType("ROBOT"); //系统发私人红包
		//packetConfig.setCtime(new Date());
		packetConfig.setRedPacketUserId(system_userId);
		packetConfig.setRedPacketStatus("UNFINISH");
		packetConfig.setRedPacketNumber(Integer.valueOf(number));//母红包数量
		//System.out.println("ctime"+packetConfig.getCtime());

		return toAjax(packetConfigService.insertPacketConfig(packetConfig));
	}

	/**
	 * 修改定时发红包配置
	 */
	@GetMapping("/edit/{redPacketConfigId}")
	public String edit(@PathVariable("redPacketConfigId") String redPacketConfigId, ModelMap mmap)
	{
		PacketConfig packetConfig = packetConfigService.selectPacketConfigById(redPacketConfigId);
		mmap.put("packetConfig", packetConfig);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存定时发红包配置
	 */
	@RequiresPermissions("module:packetConfig:edit")
	@Log(title = "定时发红包配置", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PacketConfig packetConfig)
	{		
		return toAjax(packetConfigService.updatePacketConfig(packetConfig));
	}
	
	/**
	 * 删除定时发红包配置
	 */
	@RequiresPermissions("module:packetConfig:remove")
	@Log(title = "定时发红包配置", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(packetConfigService.deletePacketConfigByIds(ids));
	}
	
}
